function choose_theme(th)
{
	window.location.href='/admin/?set-theme='+th;
}

function page_editor_rebuild_blocks(frm,templateplaces,cpage_blocks,mlangs,gdbe_styles)
{
	if (!templateplaces) return;
	var i;
	frm.ajajform("deleteWidgetsInLayoutBlock",".af-peditcol2");
	var arr=[];
	if (!mlangs) mlangs=[""];
	for (var li=0;li<mlangs.length;li++) {
		var lang=mlangs[li];
		for (i=0;i<templateplaces.length;i++) {
			var tp=templateplaces[i];
			var ptc=tp.place_type_code;
			var wysiwyg=false;
			switch (ptc) {
				case "image":
				case "tmplimage":
					arr[i]=page_editor_rebuild_blocks__image(frm,tp,cpage_blocks[tp.code],lang);
					break;
				case "html":
				case "tmplhtml":
					wysiwyg=true;
				case "text":
				case "tmpltext":
					arr[i]=page_editor_rebuild_blocks__text(frm,tp,wysiwyg,cpage_blocks[tp.code],lang);
					break;
				case "gdbe":
				case "tmplgdbe":
					arr[i]=page_editor_rebuild_blocks__gdbe(frm,tp,cpage_blocks[tp.code],lang,gdbe_styles);
					break;
			}
		}
	}
}

function page_editor_rebuild_blocks__image(frm,tp,cpage_block,lang)
{
	return frm.ajajform("insertWidget",'.af-peditcol2',null,{
		type: "urledit",
		name: "block_"+tp.id+(lang==""?"":"_"+lang),
		label: tp.name+", "+tp.place_type_name+(lang==""?"":" ("+lang+")"),
		value: cpage_block?(lang==""?cpage_block.body:cpage_block["body_"+lang]):"",
		width: "200",
		cssclass: "afw-urledit pblock",
		change: function() {}
	});

}

function page_editor_rebuild_blocks__text(frm,tp,wysiwyg,cpage_block,lang)
{
	var ah=tp.admin_height;
//	var wysiwyg=(ptc=="html" || ptc=="tmplhtml")?true:false;
	if (wysiwyg && ah<5) ah=5;
	return frm.ajajform("insertWidget",'.af-peditcol2',null,{
		type: ah==1?"textedit":"textareaedit",
		name: "block_"+tp.id+(lang==""?"":"_"+lang),
		label: tp.name+", "+tp.place_type_name+(lang==""?"":" ("+lang+")"),
		value: cpage_block?(lang==""?cpage_block.body:cpage_block["body_"+lang]):"",
		width: "100%",
		cssclass: ah==1?"afw-textedit pblock":"afw-textareaedit pblock",
		css: '/site.css',
		height: ah*15,
		wysiwyg: wysiwyg,
		change: function() {}/*,
		htmltemplate: '<div class="pblock-label"></div><div class="pblock-edit"><textarea/><div class="pblock-error"></div></div></div>'*/
	});
}

function page_editor_rebuild_blocks__gdbe(frm,tp,cpage_block,lang,styles)
{
//	var wysiwyg=(ptc=="html" || ptc=="tmplhtml")?true:false;
	return frm.ajajform("insertWidget",'.af-peditcol2',null,{
		type: "gdbeditor",
		name: "block_"+tp.id+(lang==""?"":"_"+lang),
		label: tp.name+", "+tp.place_type_name+(lang==""?"":" ("+lang+")"),
		value: cpage_block?(lang==""?cpage_block.body:cpage_block["body_"+lang]):"",
		width: "100%",
		styles: styles,
		cssclass: "afw-gdbeditor pblock"
	});
}

function uploader_init(post_params)
{
	post_params.downscale=$(".upload-many-block .downscale").attr("checked")?1:0;
	post_params.max_x=$(".upload-many-block .max-x").attr("value");
	post_params.max_y=$(".upload-many-block .max-y").attr("value");
	$(".swfupload-holder").empty().html('<div id="swfupload-control"><input type="button" id="button" value="Upload"/><p id="queuestatus"></p><ol id="log"></ol></div>');
	var upl=$('#swfupload-control').swfupload({
		upload_url: "/admin-filebrowser__upload/",
		post_params: post_params,
		use_query_string : true,
		file_post_name: 'Filedata',
		file_size_limit : 0,
		file_types : post_params.all?"*.*":"*.jpg;*.png;*.gif;*.JPG;*.PNG;*.GIF",
		file_types_description : post_params.all?"All files":"Image files",
		file_upload_limit : 0,
		flash_url : "/shared/jquery.swfupload/swfupload/swfupload.swf",
		button_image_url : '/shared/swfupload_upload.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0]
	});
	upl.bind('fileQueued', function(event, file){
		var listitem='<li id="'+file.id+'" >'+
			'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
			'<div class="progressbar" ><div class="progress" ></div></div>'+
			'<p class="status" >Pending</p>'+
			'<span class="cancel" >&nbsp;</span>'+
			'</li>';
		$('#log').append(listitem);
		$('li#'+file.id+' .cancel').bind('click', function(){
			var swfu = $.swfupload.getInstance('#swfupload-control');
			swfu.cancelUpload(file.id);
			$('li#'+file.id).slideUp('fast');
		});
		// start the upload since it's queued
		$(this).swfupload('startUpload');
	});
	upl.bind('fileQueueError', function(event, file, errorCode, message){
			alert('Size of the file '+file.name+' is greater than limit');
	});
	upl.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
			$('#queuestatus').text('Files Selected: '+numFilesSelected+' / Queued Files: '+numFilesQueued);
	});
	upl.bind('uploadStart', function(event, file){
		$('#log li#'+file.id).find('p.status').text('Uploading...');
		$('#log li#'+file.id).find('span.progressvalue').text('0%');
		$('#log li#'+file.id).find('span.cancel').hide();
		return true;
	});
	upl.bind('uploadProgress', function(event, file, bytesLoaded){
		//Show Progress
		var percentage=Math.round((bytesLoaded/file.size)*100);
		$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
		$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
	});
	upl.bind('uploadSuccess', function(event, file, serverData){
		var item=$('#log li#'+file.id);
		item.find('div.progress').css('width', '100%');
		return true;
	});
	upl.bind('uploadComplete', function(event, file){
		$(this).swfupload('startUpload');
//		upl.swfupload("uploadStart");
		return true;
	});
	upl.bind('uploadError', function(event, file, errorCode, message){
		alert('Error uploading file '+file.name+' code='+errorCode+', message='+message);
	});
}

function openFileBrowser(field_name, url, type, win,submode)
{
	console.log({func:"openFileBrowser",field_name:field_name,url:url,type:type,win:win,submode:submode});
	var openurl="/admin_popup-filebrowser/?target="+(submode||"tinymce");
	if (type=="image") openurl+="&file_type_id=1";
	if (url) {
		var arr=url.match(/.*\/(\d+)\//);
		if (arr) openurl+="&file_group_id="+arr[1];
	}
	tinyMCE.activeEditor.windowManager.open({
		file : openurl,
		title : 'My File Browser',
		width : 800,
		height : 500,
		resizable : "yes",
		inline : "yes",
		close_previous : "no"
	}, {
		window : win,
		input : field_name
	});
    return false;
}

function filemanager_file_group_edit(ref,folders_tree,srcparams)
{
	var d=$('<div title="'+(ref.id?"Редактирование":"Создание")+' раздела"><div id="file-group-edit-form"></div></div>').appendTo($("body"));
	var params={
		mode: "file_group_save",
		ajaj: 1,
		parent_id: ref.parent_id,
		id: ref.id||""
	};
	for (var k in srcparams) params[k]=srcparams[k];
	var frmopt={
		submit: {
			url: "/admin-filebrowser/",
			ajaj: true,
			params: params,
			value: ref.id?"Сохранить":"Создать"
		}
	};
	var frm=$("#file-group-edit-form").ajajform(frmopt);
	var folders_tree2=[{id:"",name:""}];
	for (var i1=0,s1=folders_tree.length;i1<s1;i1++) {
		x=folders_tree[i1];
		folders_tree2.push({id:x.id,name:x.name});
	}
//	folders_tree.forEach(function(x) {folders_tree2.push({id:x.id,name:x.name});});
/*	frm.ajajform("insertWidget",null,null,{
		type: "select",
		label: "Родительский раздел",
		name: "parent_id",
		width: 300,
		values: folders_tree2,
		value: ref.parent_id
	});*/

	frm.ajajform("insertWidget",null,null,{
		type: "textedit",
		label: "Название",
		name: "name",
		width: 300,
		value: ref.name
	});

	d.dialog({
		modal: true,
		width: 550,
		close: function() { d.dialog("destroy");d.remove(); }
	});
}

